package com.meihua.code.io;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: code
 * @description:
 * @author: meihua
 * @created: 2021/08/06 09:55
 */
public class TestNIOSocket {

    private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(5, 10, 100, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));

    private static final  int  PROT =10101;

    public static void main(String[] args) {

        TestNIOSocket testSocket = new TestNIOSocket();
        new Thread(()->testSocket.read()).start();
       // testSocket.write();
    }


    public void read() {
        ServerSocket server = null;
        try {
            server = new ServerSocket(PROT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true){
                try {
                Socket accept = server.accept();
                System.out.println("收到事件");
                EXECUTOR.execute(()->{
                    try {
                        System.out.println(accept.getInetAddress()+"已成功连接到此台服务器上。");

                        DataInputStream br = new DataInputStream(accept.getInputStream());
                        //读取客户端发送来的消息
                        while (true){
                            int read = accept.getInputStream().read();
                            System.out.println(read);
                           // String s = br.readUTF();
                         //   System.out.println(s);

                            new DataOutputStream(accept.getOutputStream()).writeUTF(""+read);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                   });
                }catch (IOException e){
                    e.printStackTrace();
                }
        }
    }

    public void write(){
        Socket s = null;
        InputStream is=null;
        OutputStream os =null;
        BufferedWriter bw =null;
        DataOutputStream pw =null;
        try {
            s = new Socket("localhost",PROT);
            is = s.getInputStream();
            os = s.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os));
            pw=new DataOutputStream(s.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }


        Scanner sc = new Scanner(System.in);

        while (sc.hasNextLine()){
            String next = sc.next();
            //向服务器端发送一条消息
            try {
                pw.writeUTF(next);
                pw.flush();
                //读取服务器返回的消息

                DataInputStream br = new DataInputStream(s.getInputStream());
                //读取客户端发送来的消息
                System.out.println(br.readUTF());

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
